//************************************************************
//	COPYRIGHT 2011 Birthin' Hips - ALL RIGHTS RESERVED
//************************************************************
//
//	$Id: ConsumerThread.java$
//
//	$Date: 12/13/11$
//
//	Reason:  The Consumer Thread pulls messages from the read queue and inserts them into the write queue
//
//*************************************************************

/** $Author: Colton Presler, Ross Kahn, Elysia Haight$ */
/** $Revision: $ */

package com;

//==============================================================
public class ConsumerThread implements Runnable{

	private boolean running;		// True if this thread is still running
	private long delay;				// Time delay between pulls off the read queue
	
	/**
	 * Class constructor. Sets running to true, so that the thread will run forever until running is set to false.
	 * @param delay		Time delay in milliseconds that the consumer sleeps before reading off the read queue
	 */
	//----------------------------------------------------------
	public ConsumerThread(long delay){
		running = true;
		this.delay = delay;
	}
	
	@Override
	/**
	 * Run method for this thread. Loops forever if running is set to true. Pulls a message off the read queue
	 * (located in MessageQueue.java), and if that message is non-null, will reflect it back to the write queue.
	 * Thread will then sleep for a specified amount of time before repeating.
	 */
	//----------------------------------------------------------
	public void run(){
		while(running)
		{
			Message msg = MessageQueue.pullFromReadQueue();
			
			if(msg != null)
			{
				MessageQueue.insertToWriteQueue(msg);
			}
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Sets running to false, which will end the while loop in run()
	 */
	//----------------------------------------------------------
	public void stop(){
		running = false;
	}

}
